<template>
    <div>
        <b-dl v-for="item in list" :key="item.id" :obj="item"></b-dl>
    </div>
</template>
<script>
import bDl from '../components/b-dl'
export default {
    components:{
        bDl
    },
    created(){
        this.getList();
        this.$bus.$off('add');
        this.$bus.$on('add',(state,id) =>{
            let ind = this.list.findIndex(item => item.id === id);
            this.list[ind].state = state;
            // state  true  加入的状态  存
            //        false 反之 删
            if(state){
                this.addList.push(this.list[ind]);
            }else{
                let ind = this.addList.findIndex(item => item.id === id);
                this.addList.splice(ind,1);
            }
            localStorage.setItem('addList',JSON.stringify(this.addList));
        })
    },
    methods:{
        getList(){
            let shelfList = JSON.parse(localStorage.getItem('addList'))|| [];
            this.$http.get('/api/list').then((res) => {
                if(res.data.code === 1){
                    let data = res.data.list.data;
                    data.forEach(item => {
                        item.state = false;
                        shelfList.forEach(v => {
                            if(item.id === v.id){
                                item.state = true;
                            }
                        })
                    })

                    this.list = data;
                }
            })
        }
    },
    data(){
        return {
            list:[],
            addList:[]
        }
    }
}
</script>